package org.advanced.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author John.zhang
 * @version Id: Server, v 1.0 2020/12/14 15:29 ZhiYong Exp $
 */
public class Server {

    public static void main(String[] args) {

        try {
            //暴露一个服务，提供一个监听，等待客户链接
            ServerSocket serverSocket = new ServerSocket(8081);
            Socket socket = serverSocket.accept();
            //从网络上拿到一个输入流
            InputStream inputStream = socket.getInputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }
            inputStream.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
